﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_Province_EditPro
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Dim level As String = -1
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                getParent()
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub getParent()
        drlParent.Items.Clear()
        drlParent.DataValueField = "Id"
        drlParent.DataTextField = "Title"
        RecursiveFillTree(0)
        drlParent.Items.Insert(0, New ListItem("-Chọn danh mục địa danh cấp cha-", "0"))

    End Sub
    Private Sub RecursiveFillTree(ByVal parentID As Integer)
        level += 1
        'on the each call level increment 1
        Dim appender As New StringBuilder()

        For j As Integer = 0 To level - 1

            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;")
        Next
        If level > 0 Then
            appender.Append("|__")
        End If

        Dim data As New VSEntities
        Dim dv As List(Of Province) = (From q In data.Provinces Where q.ParentId = parentID Select q).ToList
        Dim i As Integer

        If dv.Count > 0 Then
            For i = 0 To dv.Count - 1
                drlParent.Items.Add(New ListItem(Server.HtmlDecode(appender.ToString() + dv.Item(i).Title.ToString()), dv.Item(i).Id.ToString()))
                RecursiveFillTree(Integer.Parse(dv.Item(i).Id.ToString()))
            Next
        End If

        level -= 1
        'on the each function end level will decrement by 1
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As Province = (From q In data.Provinces Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                txtCode.Text = IIf(IsNothing(p.Code) = True, "", p.Code)
                txtTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                drlParent.SelectedValue = IIf(IsNothing(p.ParentId) = True, "", p.ParentId)
                txtSort.Text = IIf(IsNothing(p.Sort) = True, "", p.Sort)
                txtDiachi.Text = IIf(IsNothing(p.DiaChi) = True, "", p.DiaChi)
                txtDienthoai.Text = IIf(IsNothing(p.DienThoai) = True, "", p.DienThoai)
                txtFax.Text = IIf(IsNothing(p.Fax) = True, "", p.Fax)
                txtEmail.Text = IIf(IsNothing(p.Email) = True, "", p.Email)
                txtWebsite.Text = IIf(IsNothing(p.Website) = True, "", p.Website)
                txtSongaytrahoso.Text = IIf(IsNothing(p.SoNgayTraHoSo) = True, "", p.SoNgayTraHoSo)
                txtBienNhan.Text = IIf(IsNothing(p.BienNhan) = True, "", p.BienNhan)
                txtGioBienNhan.Text = IIf(IsNothing(p.GioBienNhan) = True, "", p.GioBienNhan)
                btnSave.Visible = HasPermission(Function_Name.Province, Session("RoleID"), 0, Audit_Type.Edit)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As Province = (From q In data.Provinces Where q.Id = hidID.Value).SingleOrDefault
            Try
                p.Id = hidID.Value
                p.Code = txtCode.Text.Trim()
                p.Title = txtTitle.Text.Trim()

                If (p.ParentId <> drlParent.SelectedValue) Then
                    Dim parent As Integer = CInt(drlParent.SelectedValue)
                End If
                p.ParentId = CInt(drlParent.SelectedValue)
                p.Sort = txtSort.Text.Trim()
                p.DiaChi = txtDiachi.Text.Trim()
                p.DienThoai = txtDienthoai.Text.Trim()
                p.Fax = txtFax.Text.Trim()
                p.Email = txtEmail.Text.Trim()
                p.Website = txtWebsite.Text.Trim()
                p.SoNgayTraHoSo = txtSongaytrahoso.Text.Trim()
                p.GioBienNhan = txtGioBienNhan.Text.Trim()
                p.BienNhan = txtBienNhan.Text()
                data.SaveChanges()
                'Insert_App_Log("Insert Province:" & txtTitle.Text.Trim & "", Function_Name.Province, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Province/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ShowData()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
